package com.helloniu.fl {

import com.helloniu.fl.util.Tool;
import flash.net.NetConnection;
import flash.net.Responder;
import flash.events.*;

/**
private var ncHandler:NCHandler = null;
private var sohandler:SOHandler = null;

private function conenct(e:*):void {
    ncHandler = new NCHandler();
    sohandler = new SOHandler();
    ncHandler.setURL("rtmp://localhost/SOSample/12344");
    ncHandler.connect(connectSuccess);
}

private function connectSuccess(handler:NCHandler) {
    sohandler.setName("ppSO");
    sohandler.setNC(handler.getNC());
    sohandler.setStatusListener(statusChange);
    sohandler.connect();
}

function statusChange(asoHandler:SOHandler, pname:String, oldValue:*, newValue:*):void {
    if(pname == null) {
        return;
    }

    newValue = asoHandler.getProperty(pname);

    if(pname == "a") {
        //processAChange(newValue)
    }
    else if(pname == "b") {
        //processBChange(newValue)
    }
}

***/
public class NCHandler {
    public static const CONNECTION_SUCCESS:String = "HELLONIU_SUCCESS";

    public function NCHandler() {
        netConnection = new NetConnection();
    }

    public function setURL(aurl:String):void {
        url = aurl;
    }

    public function getNetConnection():NetConnection {
        return netConnection
    }

    public function getNC():NetConnection {
        return netConnection
    }

    public function call(command:String, responder:Responder, ... arguments):void {
    	if(arguments.length == 0) {
            netConnection.call(command, responder);
        }
    	else if(arguments.length == 1) {
            netConnection.call(command, responder, arguments[0]);
        }
    	else if(arguments.length == 2) {
            netConnection.call(command, responder, arguments[0], arguments[1]);
        }
    	else if(arguments.length == 3) {
            netConnection.call(command, responder, arguments[0], arguments[1], arguments[2]);
        }
    	else  {
            Tool.alert("not support too many params!");
        }
    }

    public function addClosedListener(aclosedListener:Function):void {
        this.closedListener = aclosedListener;
    }

    public function connect(aconnectSuccess:Function):void {
        connectSuccess = aconnectSuccess;
        //netConnection.client = this;

        var events:Array = eventsToHandle;
        var n:int = events.length;

        for(var i:int = 0; i < n; i++) {
            netConnection.addEventListener(events[i], eventHandler, false, 0, false);
        }

        try {
            netConnection.connect(url);
        }
        catch(error:Error) {
            // trace(connect error);
        }
    }

    public function disconnect():void {
        var events:Array = eventsToHandle;
        var n:int = events.length;

        for(var i:int = 0; i < n; i++) {
            netConnection.removeEventListener(events[i], eventHandler);
        }

        netConnection.close();
    }

    /**
     *  Strings of the events to listen for.
     */
    private function get eventsToHandle():Array {
        return [ NetStatusEvent.NET_STATUS, AsyncErrorEvent.ASYNC_ERROR,
            SecurityErrorEvent.SECURITY_ERROR, IOErrorEvent.IO_ERROR ];
    }

    /**
     * Net status handler.
     */
    private function eventHandler(event:Event):void {
        var etype:String = event.type;
        var events:Array = eventsToHandle;

        if(etype != events[0]) {
            Tool.traceT("=1=nc error: ");
            // trace(error found);

            if(closedListener != null) {
            	(closedListener as Function).call(null, this);
            }

            return;
        }

        var statusLevel:String = (event as NetStatusEvent).info.level;

        if(statusLevel == "error") {
            // trace(net status error);
            Tool.alert("=2=nc error: ");

            if(closedListener != null) {
            	(closedListener as Function).call(null, this);
            }

            return;
        }

        var statusCode:String = (event as NetStatusEvent).info.code;
        var successCode:String = "NetConnection.Connect.Success";
        var closedCode:String = "NetConnection.Connect.Closed";

        if(statusCode == successCode) {
            if(connectSuccess != null) {
                (connectSuccess as Function).call(null, this);
            }
        }
        else if(statusCode == closedCode) {
			Tool.traceT("==nc closed: ");
            //Alert.show("==NC=close!");
            if(closedListener != null) {
                (closedListener as Function).call(null, this);
            }
        }
        else {
        }
    }

    public function setConfID(aconfID:String):void {
    	confID = aconfID;
    }

    public function setUserID(auserID:String):void {
    	userID = auserID;
    }

    public function getConfID():String {
    	return confID;
    }

    public function getUserID():String {
    	return userID;
    }

    private var netConnection:NetConnection;
    private var connectSuccess:Function;
    private var closedListener:Function;
    private var url:String;
    private var confID:String;
    private var userID:String;

}
}